﻿using System;

namespace Wild2.Models.Convertors
{
    public static class Helper
    {
        /// <summary>
        /// Try cast <paramref name="obj"/> value to type <typeparamref name="T"/>,
        /// if can't will return default(<typeparamref name="T"/>)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T To<T>(this object obj)
        {
            return To(obj, default(T));
        }

        /// <summary>
        /// Try cast <paramref name="obj"/> value to type <typeparamref name="T"/>,
        /// if can't will return <paramref name="defaultValue"/>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static T To<T>(this object obj, T defaultValue)
        {
            if (obj == null)
                return defaultValue;

            if (obj is T)
                return (T)obj;

            Type type = typeof(T);

            // Place convert to reference types here

            if (type == typeof(string))
            {
                return (T)(object)obj.ToString();
            }

            Type underlyingType = Nullable.GetUnderlyingType(type);
            if (underlyingType != null)
            {
                return To(obj, defaultValue, underlyingType);
            }

            return To(obj, defaultValue, type);
        }

        private static T To<T>(object obj, T defaultValue, Type type)
        {
            // Place convert to sructures types here

            if (type == typeof(int))
            {
                int intValue;
                if (int.TryParse(obj.ToString(), out intValue))
                    return (T)(object)intValue;
                return defaultValue;
            }

            if (type == typeof(long))
            {
                long intValue;
                if (long.TryParse(obj.ToString(), out intValue))
                    return (T)(object)intValue;
                return defaultValue;
            }

            if (type == typeof(bool))
            {
                bool bValue;
                if (bool.TryParse(obj.ToString(), out bValue))
                    return (T)(object)bValue;
                return defaultValue;
            }

            if (type.IsEnum)
            {
                if (Enum.IsDefined(type, obj))
                    return (T)Enum.Parse(type, obj.ToString());
                return defaultValue;
            }

            throw new NotSupportedException(string.Format("Couldn't parse to Type {0}", typeof(T)));
        }

        public static int WordCount(this String str)
        {
            return str.Split(new[] { ' ', '.', '?', '!' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }

        public static String Translit(this String str)
        {
            return Text.Helper.Translit(str);
        }

        public static String BBCode(this String str, bool img)
        {
            return Text.Helper.Bbcode(str, img);
        }

        public static String Nbsp(this String str)
        {
            return Text.Helper.nbsp(str);
        }

        private const string RussianChars = "»,«,',\", ,А,Б,В,Г,Д,Е,Ё,Ж,З,И,Й,К,Л,М,Н,О,П,Р,С,Т,У,Ф,Х,Ц,Ч,Ш,Щ,Ъ,Ы,Ь,Э,Ю,Я,а,б,в,г,д,е,ё,ж,з,и,й,к,л,м,н,о,п,р,с,т,у,ф,х,ц,ч,ш,щ,ъ,ы,ь,э,ю,я";
        private const string EnglishChars = "-,-,-,-,-,A,B,V,G,D,E,E,Zh,Z,I,J,K,L,M,N,O,P,R,S,T,U,F,H,C,Ch,Sh,Sch,,I,,E,U,Ya,a,b,v,g,d,e,e,zh,z,i,j,k,l,m,n,o,p,r,s,t,u,f,h,c,ch,sh,sch,,i,,e,u,ya";

        public static String TranslitUri(this String russianString)
        {
            String s = russianString;
            string[] rChars = RussianChars.Split(new[] { ',' });
            string[] eChars = EnglishChars.Split(new[] { ',' });
            for (int i = 0; i < rChars.Length; i++)
            {
                s = s.Replace(rChars[i], eChars[i]);
            }
            return s;
        }
    }
}